#! /usr/bin/env python
#coding=utf-8

"""

    2011 Boris Tatarintsev

    Class which manages local highscores i.e.
    save score table on disk, load score table from disk, etc.

"""

import pickle
import strings
import os

from hsbasic import HSBasic

class HSLocal(HSBasic):

    SCORE_FILENAME = os.path.join('..', 'highscores')
    SCORES_COUNT = 25
    
    def __init__(self):
        HSBasic.__init__(self)        

    def getRawData(self):
        data = []
        try:
            f = open(HSLocal.SCORE_FILENAME, 'rb')
            data = f.read()
            f.close()
        except IOError:
            # error reading file, try to create a new empty
            # high scores data file
            self.addNewScoreTable(strings.STR_MAIN_MENU_STANDART, capacity = HSLocal.SCORES_COUNT)
            self.addNewScoreTable(strings.STR_MAIN_MENU_SURVIVE, capacity = HSLocal.SCORES_COUNT)
            stream = self.dumpScoresData()
            f = open(HSLocal.SCORE_FILENAME, 'wb')
            f.write(stream.getvalue())
            f.close()                
        return data

    def saveRawData(self):
        data = self.dumpScoresData()
        f = open(HSLocal.SCORE_FILENAME, 'wb')
        f.write(data.getvalue())
        f.close()
